import { _decorator, animation, Animation, Component, Node, PhysicsSystem2D, Rect } from 'cc';
import { Actor } from './Actor';
import { Enemy } from './Enemy/Enemy';
import { SkillManager } from '../Skill/SkillManager';
const { ccclass, property } = _decorator;

@ccclass('AnimTrigger')
export class AnimTrigger extends Component {

    @property(Actor) actor:Actor = null;
    animation:Animation = null;

    protected start(): void {
        this.animation = this.node.getComponent(Animation);
        this.animation.on(Animation.EventType.FINISHED,this.AnimationTrigger, this);
    }
    public AnimationTrigger(): void {
        this.actor.stateMgr.currState.AnimationFinishTrigger();
    }
    AttackTrigger(){
        const pos = this.actor.AttackCheck.getWorldPosition();
        //偏移问题
        const box = new Rect(pos.x+(this.actor.facingDir-1)*this.actor.AttackCheckR/2,pos.y, this.actor.AttackCheckR, this.actor.AttackCheckR);
        const colliderList = PhysicsSystem2D.instance.testAABB(box); 
        for (const collider of colliderList) {
            const enemyComponent = collider.getComponent(Enemy);
            if (enemyComponent) {
                console.info(collider.name);
                enemyComponent.damage();
            }
        }
    }
    ThrowSword(){
        SkillManager.inst.sword_Skill.CreateSword();
    }
}


